/*
ONEGAME
Copyright (C) 2009  Pablo Caballero

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package one.dialog;

import java.util.Vector;
import swing.JCenteredDialog;

/**
 * Dialog of properties
 * @author Pablo Caballero
 * @version 1.0 24-jun-2009
 */
public class PropertiesDlg extends JCenteredDialog implements IEditorTableModelEventSystem {

	/** Creates new form PropertiesDlg */
	public PropertiesDlg(java.awt.Window parent) {
		super(parent);
		initComponents();

		setSelectedItemTitle("");
		setSelectedItemDescription("");
	}

	/**
	 * Gets the model of the table
	 * @return the model of the table
	 */
	public DefaultEditorTableModel getModel() {
		return (DefaultEditorTableModel) table.getModel();
	}

	/**
	 * Sets the model of the table
	 * @param newmodel the model of the table
	 */
	public void setModel(DefaultEditorTableModel newmodel) {
		table.setModel(newmodel);
	}

	/**
	 * Sets the text of the label of the Selected-Item-Title
	 * @param selectedItemTitle the new text for the label
	 */
	public void setSelectedItemTitle(String selectedItemTitle) {
		lblSelectedItemTitle.setVisible(selectedItemTitle.length() > 0);
		lblSelectedItemTitle.setText(selectedItemTitle);
	}

	/**
	 * Sets the text of the label of the Selected-Item-About
	 * @param selectedItemTitle the new text for the label
	 */
	public void setSelectedItemDescription(String selectedItemDescription) {
		lblSelectedItemDescription.setVisible(selectedItemDescription.length() > 0);
		lblSelectedItemDescription.setText(selectedItemDescription);
	}

	/**
	 * Updates the help system
	 */
	private void updateHelp() {
		Vector<FieldEditor> fields = getModel().getFields();
		int[] rows = table.getSelectedRows();
		if (rows.length == 1) {
			setSelectedItemTitle(fields.get(rows[0]).getHelpTitle());
			setSelectedItemDescription(fields.get(rows[0]).getHelpDescription());
		} else {
			setSelectedItemTitle("");
			setSelectedItemDescription("");
		}
	}

	/**
	 * Hides the window
	 */
	private void close() {
		dispose();
	}

	public void addPropertiesListener(IPropertiesDialogEventListener l) {
		getModel().addPropertiesListener(l);
	}

	public void removePropertiesListener(IPropertiesDialogEventListener l) {
		getModel().removePropertiesListener(l);
	}

	public void firePropertiesChanged() {
		getModel().firePropertiesChanged();
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();
        lblSubtitle = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        lblSelectedItemTitle = new javax.swing.JLabel();
        cmdHelp = new javax.swing.JButton();
        cmdClose = new javax.swing.JButton();
        scrSelectedItemDescription = new javax.swing.JScrollPane();
        lblSelectedItemDescription = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });
        getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.LINE_AXIS));

        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jSplitPane1.setResizeWeight(0.75);

        jPanel1.setBackground(new java.awt.Color(204, 204, 204));

        table.setModel(new DefaultEditorTableModel());
        table.getTableHeader().setReorderingAllowed(false);
        table.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableMouseClicked(evt);
            }
        });
        table.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tableKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(table);

        lblSubtitle.setFont(new java.awt.Font("Verdana", 1, 11));
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("one/language/labels"); // NOI18N
        lblSubtitle.setText(bundle.getString("PROPERTIESDLG_SUBTITLE")); // NOI18N
        lblSubtitle.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(lblSubtitle, javax.swing.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(lblSubtitle, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 378, Short.MAX_VALUE))
        );

        jSplitPane1.setLeftComponent(jPanel1);

        lblSelectedItemTitle.setFont(new java.awt.Font("Verdana", 1, 11));
        lblSelectedItemTitle.setText("lblSelectedItemTitle");

        cmdHelp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/one/image/help_25x25.png"))); // NOI18N
        cmdHelp.setBorderPainted(false);
        cmdHelp.setContentAreaFilled(false);
        cmdHelp.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                cmdHelpMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                cmdHelpMouseExited(evt);
            }
        });

        cmdClose.setText(bundle.getString("CLOSE_BUTTON")); // NOI18N
        cmdClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdCloseActionPerformed(evt);
            }
        });

        scrSelectedItemDescription.setBorder(null);

        lblSelectedItemDescription.setBackground(new java.awt.Color(235, 233, 237));
        lblSelectedItemDescription.setColumns(20);
        lblSelectedItemDescription.setEditable(false);
        lblSelectedItemDescription.setFont(new java.awt.Font("Verdana", 0, 11));
        lblSelectedItemDescription.setLineWrap(true);
        lblSelectedItemDescription.setRows(2);
        lblSelectedItemDescription.setText("lblSelectedItemDescription");
        scrSelectedItemDescription.setViewportView(lblSelectedItemDescription);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(lblSelectedItemTitle)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 169, Short.MAX_VALUE)
                        .addComponent(cmdHelp, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(scrSelectedItemDescription, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(cmdClose)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblSelectedItemTitle)
                    .addComponent(cmdHelp, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cmdClose, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(scrSelectedItemDescription, javax.swing.GroupLayout.DEFAULT_SIZE, 43, Short.MAX_VALUE))
                .addContainerGap())
        );

        jSplitPane1.setRightComponent(jPanel2);

        getContentPane().add(jSplitPane1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void cmdHelpMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cmdHelpMouseEntered
		cmdHelp.setBorderPainted(true);
		cmdHelp.setContentAreaFilled(true);
}//GEN-LAST:event_cmdHelpMouseEntered

	private void cmdHelpMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cmdHelpMouseExited
		cmdHelp.setBorderPainted(false);
		cmdHelp.setContentAreaFilled(false);
}//GEN-LAST:event_cmdHelpMouseExited

	private void tableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableMouseClicked
		updateHelp();
	}//GEN-LAST:event_tableMouseClicked

	private void tableKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tableKeyPressed
		updateHelp();
	}//GEN-LAST:event_tableKeyPressed

	private void cmdCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCloseActionPerformed
		close();
	}//GEN-LAST:event_cmdCloseActionPerformed

	private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
		close();
	}//GEN-LAST:event_formWindowClosing

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdClose;
    private javax.swing.JButton cmdHelp;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTextArea lblSelectedItemDescription;
    private javax.swing.JLabel lblSelectedItemTitle;
    private javax.swing.JLabel lblSubtitle;
    private javax.swing.JScrollPane scrSelectedItemDescription;
    private javax.swing.JTable table;
    // End of variables declaration//GEN-END:variables
}
